CSharpTest.Net
EventHandler Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Delegates Namespace > EventHandlerForControl<TEventArgs> Class : EventHandler Method

sender
args

Glossary Item Box

Handles the 'magic' of safely invoking the delegate on the control without producing a dead-lock.

Syntax

Visual Basic (Declaration) 
Public Sub EventHandler( _
   ByVal sender As Object, _
   ByVal args As TEventArgs _
) 
C# 
public void EventHandler( 
   object sender,
   TEventArgs args
)

Parameters

sender
args

Example

Library/Library.Test/TestDelegates.cs

C#Copy Code
EventHandlerForActiveControl<VerifiedReceiptEventArgs> handler;
handler = new EventHandlerForActiveControl<VerifiedReceiptEventArgs>(_form, VerifiedReceipt);
handler = new EventHandlerForActiveControl<VerifiedReceiptEventArgs>(_form, new VerifiedReceiptEventHandler(handler.EventHandler));
VerifiedReceiptEventArgs args = new VerifiedReceiptEventArgs();
_form.Shown += delegate(object s, EventArgs e) { System.Threading.Thread.Sleep(100); _form.Close(); };
_thread.Start();
int total = 0, countOnThread = 0, countOffThread = 0;
try
{
    for (total = 1; total < 1000000; total++)
    {
        handler.EventHandler(null, args.Reset());
        if (args.OnThread)
        {
            Assert.IsTrue(args.Received);
            countOnThread++;
        }
        else
        {
            Assert.IsFalse(args.Received);
            countOffThread++;
        }
        //Assert.IsTrue(args.OnThread);
        //Console.Error.WriteLine("Threaded = {0}", args.OnThread);
        if (_form.IsDisposed)
            break;
    }
    Assert.AreEqual(total, countOnThread + countOffThread);
    Assert.IsTrue(_form.IsDisposed);
    Assert.AreNotEqual(0, countOnThread);
    Assert.AreNotEqual(0, countOffThread);
}
catch (Exception e)
{
    Console.Error.WriteLine(e.ToString());
    throw;
}
finally
{
    _thread.Join();
}
VB.NETCopy Code
Dim handler As EventHandlerForActiveControl(Of VerifiedReceiptEventArgs)
handler = New EventHandlerForActiveControl(Of VerifiedReceiptEventArgs)(_form, VerifiedReceipt)
handler = New EventHandlerForActiveControl(Of VerifiedReceiptEventArgs)(_form, New VerifiedReceiptEventHandler(handler.EventHandler))
Dim args As New VerifiedReceiptEventArgs()
_form.Shown += Function(s As Object, e As EventArgs) Do
    System.Threading.Thread.Sleep(100)
    _form.Close()
End Function
_thread.Start()
Dim total As Integer = 0, countOnThread As Integer = 0, countOffThread As Integer = 0
Try
    total = 1
    While total < 1000000
        handler.EventHandler(Nothing, args.Reset())
        If args.OnThread Then
            Assert.IsTrue(args.Received)
            System.Math.Max(System.Threading.Interlocked.Increment(countOnThread),countOnThread - 1)
        Else
            Assert.IsFalse(args.Received)
            System.Math.Max(System.Threading.Interlocked.Increment(countOffThread),countOffThread - 1)
        End If
        'Assert.IsTrue(args.OnThread);
        'Console.Error.WriteLine("Threaded = {0}", args.OnThread);
        If _form.IsDisposed Then
            Exit While
        End If
        System.Math.Max(System.Threading.Interlocked.Increment(total),total - 1)
    End While
    Assert.AreEqual(total, countOnThread + countOffThread)
    Assert.IsTrue(_form.IsDisposed)
    Assert.AreNotEqual(0, countOnThread)
    Assert.AreNotEqual(0, countOffThread)
Catch e As Exception
    Console.[Error].WriteLine(e.ToString())
    Throw
Finally
    _thread.Join()
End Try

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys